package com.winderinfo.yikaotianxia.ui.collection;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class CollectionActivity extends BaseActivity implements Contract.View {

    @BindView(R.id.co_recycler)
    RecyclerView coRecycler;
    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_mycollection;
    }

    @Override
    protected void initView() {
        //实例P
        presenter = new Presenter(this);
        coRecycler.setLayoutManager(new LinearLayoutManager(this));
        spUtils = new SPUtils(this);
    }

    @Override
    protected void initData() {
        //请求收藏数据
        /**
         * @userId 用户id
         * */
        String userID = spUtils.getString("UserID", null);
        map.clear();
        map.put("userId", 1);
        presenter.onPostRequest(Api.COLLECTION, map, CollectionBean.class);
    }

    @Override
    public void onSuccess(Object data) {
        //收藏数据
        if (data instanceof CollectionBean) {
            List<CollectionBean.RowsBean> rows = ((CollectionBean) data).getRows();
            CollectionAdapter collectionAdapter = new CollectionAdapter(this, rows);
            coRecycler.setAdapter(collectionAdapter);
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }


    @OnClick(R.id.co_back)
    public void onViewClicked() {
        finish();
    }
}
